package com.dddexplained.designbootcamp.ddd;

import java.util.Objects;

public class Length {
    private double value;
    private LengthUnit unit;

    static {
        System.out.println("static block");
    }

    public Length() {
        this(0d, LengthUnit.MM);
    }
    public Length(double value, LengthUnit unit) {
        this.value = value;
        this.unit = unit;
    }

    public Length add(Length toAdd) {
        double convertedValue = toAdd.unit.convert(this.unit, toAdd.value);
        return new Length(convertedValue + this.value, this.unit);
    }

    @Override
    public String toString() {
        return "Length{" +
                "value=" + value +
                ", unit=" + unit +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Length length = (Length) o;
        return Double.compare(length.value, value) == 0 && unit == length.unit;
    }

    @Override
    public int hashCode() {
        return Objects.hash(value, unit);
    }
}
